<?php
/**
 * Created by PhpStorm.
 * User: 魏爷
 * Date: 2016/4/21
 * Time: 15:11
 */

namespace Home\Controller;


class BookManageController extends BaseController
{
    /**
     * 书籍查询
    **/
    public function bookRefer(){
        $book_name=I('book_name');
        $book_type_id=I('book_type_id');
        $book_case_id=I('book_case_id');
        $book_isbn_id=I('book_isbn_id');
        $getBookShowInfo=D('BookManage')->getBookShowInfo($book_name,$book_type_id,$book_case_id,$book_isbn_id);
        $getBookTypeTableInfo=D('BookType')->getBookTypeTableInfo();
        $getBookIsbnTableInfo=D('BookIsbn')->getBookIsbnTableInfo();
        $getBookCaseTableInfo=D('BookCase')->getBookCaseTableInfo();
        $this->assign('_getBookShowInfo',$getBookShowInfo);
        $this->assign('_getBookCaseTableInfo',$getBookCaseTableInfo);
        $this->assign('_getBookIsbnTableInfo',$getBookIsbnTableInfo);
        $this->assign('_getBookTypeTableInfo',$getBookTypeTableInfo);
        $this->display('BookManage/bookRefer');
    }
    /**
     * 书籍购买
     **/
    public function bookRadio(){
        $book_name=I('book_name');
        $book_type_id=I('book_type_id');
        $book_case_id=I('book_case_id');
        $book_isbn_id=I('book_isbn_id');
        $getBookShowInfo=D('BookManage')->getBookShowInfo($book_name,$book_type_id,$book_case_id,$book_isbn_id);
        $getBookTypeTableInfo=D('BookType')->getBookTypeTableInfo();
        $getBookIsbnTableInfo=D('BookIsbn')->getBookIsbnTableInfo();
        $getBookCaseTableInfo=D('BookCase')->getBookCaseTableInfo();
        $this->assign('_getBookShowInfo',$getBookShowInfo);
        $this->assign('_getBookCaseTableInfo',$getBookCaseTableInfo);
        $this->assign('_getBookIsbnTableInfo',$getBookIsbnTableInfo);
        $this->assign('_getBookTypeTableInfo',$getBookTypeTableInfo);
        $this->display('BookManage/bookRadio');
    }
    /**
     * 银行卡密码设置
    **/
    public function buyBankPwd(){
        $u_id=getUserId();
        $getUserInfoById=D('UserLogin')->getUserInfoById($u_id);
        $this->assign('_getUserInfoById',$getUserInfoById);
        $this->display('BookManage/buyBankPwd');
    }
    /**
     * 银行卡密码添加
    **/
    public function buyBankPwdSave(){
        $u_id=getUserId();
        $bank_pwd=I('bank_pwd');
        $bank_repwd=I('bank_repwd');
        if(empty($bank_pwd)||empty($bank_repwd)){
            $this->error('交易密码不能为空',U('Home/BookManage/buyBankPwd'));
        }
        if($bank_pwd!=$bank_repwd){
            $this->error('密码输入不一致',U('Home/BookManage/buyBankPwd'));
        }
        $res=D('UserLogin')->buyBankPwdSave($u_id,$bank_pwd);
        if($res==false){
            $this->error('交易密码设置失败');
        }else{
            $this->success('交易密码设置成功',U('Home/BookManage/bookRadio'));
        }
    }
    /**
     * 心得体会
     **/
    public function tipExperience(){
        $this->display('BookManage/tipExperience');
    }

    /**
     * 心得添加
    **/
    public function tipAdd(){
        $u_id=getUserId();
        $feel_title=I('feel_title');
        $feel_status=I('feel_status');
        $book_name=I('book_name');
        $book_page=I('book_page');
        $article_name=I('article_name');
        $feel_intor=I('feel_intor');
        $feel_content=I('feel_content');
        if(empty($feel_title)||empty($feel_status)||empty($feel_intor)||empty($feel_content)){
            $this->error('信息不能为空',U('Home/BookManage/tipExperience'));
        }
        if($feel_title==1){
            if(empty($book_name)){
                $this->error('信息不能为空',U('Home/BookManage/tipExperience'));
            }
        }
        if($feel_title==2){
            if(empty($article_name)){
                $this->error('信息不能为空',U('Home/BookManage/tipExperience'));
            }
        }
        $res=D('TipExperience')->tipAdd($u_id,$feel_title,$feel_status,$book_name,$book_page,$article_name,$feel_intor,$feel_content);
        if($res==false){
            $this->error('提交失败，请重新提交。。。。',U('Home/BookManage/tipExperience'));
        }else{
            $this->success('提交成功',U('Home/BookManage/tipExperience'));
        }
    }
    /**
     * 心得存储
    **/
    public function tipStore(){
        $feel_status=I('feel_status');
        $u_id=getUserId();
        $getTipFeelInfo=D('TipExperience')->getTipFeelInfo($u_id,$feel_status);
        $this->assign('_getTipFeelInfo',$getTipFeelInfo);
        $this->display('BookManage/tipStore');
    }
    /**
     * 心得存储详细信息
    **/
    public function tipInfo(){
        $feel_id=I('feel_id');
        $getBookAlianceById=D('TipExperience')->getBookAlianceById($feel_id);
        $this->assign('_getBookAlianceById',$getBookAlianceById);
        $this->display('BookManage/tipInfo');
    }
    /**
     * 图书借阅
    **/
    public function borrowBook(){
        $u_id=getUserId();
        $book_id=I('book_id');
        $bookNumber=D('BookManage')->getBookNumber($book_id);
        if($bookNumber==false){
            $this->error('书籍售空',null,true);
        }
        $getRealName=D('UserAdd')->getRealName($u_id);
        if($getRealName==false){
            $this->error('未实名认证',null,true);
        }
        $bookBorrow=D('Trade')->getUserInfo($u_id);
        $trueBookBorrow=D('Borrow')->getBorrowByIdBook($u_id);
        if($bookBorrow['u_book_borrow']==$trueBookBorrow){
            $this->error('已达上限',null,true);
        }
        $res=D('Borrow')->setBorrowBook($u_id,$book_id);
        if($res==false){
            $this->error('借阅失败',null,true);
        }else{
            D('BookManage')->setBookNumber($book_id);
            $this->success('借阅成功',null,true);
        }
    }
    /**
     * 图书信息查看详情
    **/
    public function bookShowInfo(){
        $book_id=I('book_id');
        $bookShowInfo=D('BookManage')->bookShowInfo($book_id);
        $getBookTypeTableInfo=D('BookType')->getBookTypeTableInfo();
        $getBookIsbnTableInfo=D('BookIsbn')->getBookIsbnTableInfo();
        $getBookCaseTableInfo=D('BookCase')->getBookCaseTableInfo();
        $this->assign('_getBookCaseTableInfo',$getBookCaseTableInfo);
        $this->assign('_getBookIsbnTableInfo',$getBookIsbnTableInfo);
        $this->assign('_getBookTypeTableInfo',$getBookTypeTableInfo);
        $this->assign('_bookShowInfo',$bookShowInfo);
        $this->display('BookManage/bookShowInfo');
    }
    /**
     * 图书购买订单
    **/
    public function buyBookInfo(){
        $u_id=getUserId();
        $book_id=I('book_id');
        $getUserInfoById=D('UserLogin')->getUserInfoById($u_id);
        $bookShowInfo=D('BookManage')->bookShowInfo($book_id);
        $this->assign('_getUserInfoById',$getUserInfoById);
        $this->assign('_bookShowInfo',$bookShowInfo);
        $this->display('BookManage/buyBook');
    }
    /**
     * 添加订单信息
    **/
    public function buyInfoAdd(){
        $u_id=getUserId();
        $u_status=I('u_status');
        $book_id=I('book_id');
        $bank_pwd=I('bank_pwd');
        $real_name=I('real_name');
        $u_address=I('u_address');
        $u_tel=I('u_tel');
        $bank=I('bank');
        $bank_number=I('bank_number');
        $buy_type=I('buy_type');
        $book_name=I('book_name');
        $book_price=I('book_price');
        if($u_status==1||empty($u_address)||empty($u_tel)){
            $this->error('请完善收货人信息');
        }
        if($u_status==3){
            $this->error('正在实名认证中，请耐心等待。。。。');
        }
        if(empty($book_id)||empty($book_name)||empty($book_price)){
            $this->error('图书信息不完全，请及时联系管理员');
        }
        if(empty($buy_type)){
            $this->error('请您选择支付方式');
        }
        if($buy_type==3){
            $this->error('请您绑定银行卡才能进行购买',U('Home/Trade/attestationInfo'));
        }
        if($buy_type==4){
            $this->error('请您设置交易密码',U('Home/BookManage/buyBankPwd'));
        }
        if($buy_type==2){
            if(empty($bank_pwd)){
                $this->error('请输入交易密码');
            }
            $bankPwd=D('UserLogin')->getBankPwd($u_id,$bank_pwd);
            if($bankPwd==false){
                $this->error('交易密码正确，请重新输入。。。。。。');
            }
        }
        $res=D('BuyBook')->buyInfoAdd($u_id,$buy_type,$real_name,$u_address,$u_tel,$book_name,$book_price,$bank,$bank_number);
        if($res==false){
            $this->error('订单提交失败，请重新提交。。。。。。。。');
        }else{
            D('BookManage')->setBookNumberById($book_id);
            $this->success('订单已提交，请耐心等待。。。。。。。。。',U('Home/BookManage/bookRadio'));
        }
    }

}